﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using PlanningGIS.ArcGIS;
using PlanningGIS.Framework;
using PlanningGIS.MapSheet;
using PlanningGIS.MetaData;
using PlanningGIS.Util.Log;
using PlanningGIS.Util.Management;

namespace PlanningGIS.Product
{
    /// <summary>
    /// 元数据处理类
    /// </summary>
    public class MetaProcess
    {
        /// <summary>
        /// 构造函数，需要指定XmlPath的值。
        /// </summary>
        public MetaProcess()
        {

        }

        /// <summary>
        /// 唯一编号
        /// </summary>
        public string ID { get; protected set; }

        /// <summary>
        /// X最小值
        /// </summary>
        public double XMIN { get; protected set; }

        /// <summary>
        /// Y最小值
        /// </summary>
        public double YMIN { get; protected set; }

        /// <summary>
        /// X最大值
        /// </summary>
        public double XMAX { get; protected set; }

        /// <summary>
        /// Y最大值
        /// </summary>
        public double YMAX { get; protected set; }

        /// <summary>
        /// 图号或测区代号
        /// </summary>
        public string MapNo { get; protected set; }

        /// <summary>
        /// 比例尺分母
        /// </summary>
        public int Scale { get; protected set; }

        /// <summary>
        /// 生产日期
        /// </summary>
        public DateTime SCDate { get; protected set; }

        /// <summary>
        /// 元数据信息
        /// </summary>
        public Dictionary<string, string> Values { get; protected set; }

        /// <summary>
        /// 要入库的文件列表
        /// </summary>
        public List<string> Files { get; protected set; }

        /// <summary>
        /// 当前选择的数据类型
        /// </summary>
        public MetaDataType SelDType { get; set; }

        /// <summary>
        /// 图号或测区代码的字段名称
        /// </summary>
        private string _F_MapNo = "";

        /// <summary>
        /// 图号或测区代码的字段名称
        /// </summary>
        public string F_MapNo
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_F_MapNo))
                    _F_MapNo = MetaHelper.FindName(SelDType.Rank.ToString(), MetaHelper.KEY).ToUpper();
                return _F_MapNo;
            }
        }

        /// <summary>
        /// 元数据Xml文件路径
        /// </summary>
        public string XmlPath { get; protected set; }

        /// <summary>
        /// 数据入库的类型
        /// </summary>
        public zjgisProType ImpType { get; protected set; }

        /// <summary>
        /// 设置与数据Xml的路径
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public bool SetXml(string xml)
        {
            XmlPath = xml;
            if (string.IsNullOrWhiteSpace(XmlPath))
            {
                LogHelper.WriteLog(new LogMessage() { Message = "元数据文件不能为空！", LogType = zjgisLogType.zjgisLogError });
                return false;
            }
            if (!File.Exists(XmlPath))
            {
                LogHelper.WriteLog(new LogMessage() { Message = "元数据文件【" + XmlPath + "】不存在，可能已被删除，或没有制作元数据，不能入库！", LogType = zjgisLogType.zjgisLogError });
                return false;
            }
            try
            {
                XDocument doc = XDocument.Load(XmlPath);
                XElement ele = doc.Element("MetaDataItems");
                if (ele == null) ele = doc.Element("MeataDataItems");//兼容之前的元数据文件
                if (ele == null)
                {
                    LogHelper.WriteLog(new LogMessage() { Message = "文件【" + XmlPath + "】不是规范的元数据", LogType = zjgisLogType.zjgisLogError });
                    return false;
                }
                Values = new Dictionary<string, string>();
                IEnumerable<XElement> nodes = ele.Elements();
                foreach (XElement nd in nodes)
                {
                    if (nd.Name.ToString().Equals("ObjectID", StringComparison.CurrentCultureIgnoreCase)) continue;
                    string v = nd.Value.Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Trim();
                    if (nd.Name.ToString().Equals(F_MapNo, StringComparison.CurrentCultureIgnoreCase)) v = v.ToUpper();
                    Values.Add(nd.Name.ToString().ToUpper(), v);
                }
            }
            catch
            {
                LogHelper.WriteLog(new LogMessage() { Message = "元数据文件【" + XmlPath + "】非标准的Xml格式，不能入库！", LogType = zjgisLogType.zjgisLogError });
                return false;
            }
            
            if (!Values.ContainsKey(F_MapNo) || string.IsNullOrWhiteSpace(Values[F_MapNo]) ||
                !Values.ContainsKey(MetaHelper.BLCFM) || string.IsNullOrWhiteSpace(Values[MetaHelper.BLCFM]) ||
                !Values.ContainsKey(MetaHelper.SCRQ) || string.IsNullOrWhiteSpace(Values[MetaHelper.SCRQ]))
            {
                LogHelper.WriteLog(new LogMessage()
                {
                    Message = string.Format("文件【{0}】中没有包含字段【{1}、{2}、{3}】，或其值为空，不能入库！", XmlPath, F_MapNo, MetaHelper.BLCFM, MetaHelper.SCRQ),
                    LogType = zjgisLogType.zjgisLogError
                });
                return false;
            }
            MapNo = Values[F_MapNo].Trim().ToUpper();
            DateTime vscrq = DateTime.Now;
            int blc = 0;
            bool suc = true;
            if (!DateTime.TryParse(Values[MetaHelper.SCRQ].Trim(), out vscrq))
            {
                LogHelper.WriteLog(new LogMessage()
                {
                    Message = "元数据【" + MapNo + "】中【" + MetaHelper.SCRQ + "】不是有效的时间！",
                    LogType = zjgisLogType.zjgisLogError
                });
                suc= false;
            }
            if (!int.TryParse(Values[MetaHelper.BLCFM].Trim(), out blc))
            {
                LogHelper.WriteLog(new LogMessage()
                {
                    Message = "元数据【" + MapNo + "】中【" + MetaHelper.BLCFM + "】不是有效的数字！",
                    LogType = zjgisLogType.zjgisLogError
                });
                suc = false;
            }
            if (!suc) return false;
            SCDate = vscrq;
            Scale = blc;
            if (SelDType.Rank == zjgisRankType.TF)
            {
                if (!JHBHelper.CheckCode(MapNo, blc))
                {
                    LogHelper.WriteLog(new LogMessage() { Message = "没有在结合表中找到文件【" + XmlPath + "】对应的图号，不能入库！", LogType = zjgisLogType.zjgisLogError });
                    return false;
                }
            }
            string fidir = System.IO.Path.GetDirectoryName(XmlPath);
            Files = MetaHelper.GetProductFils(fidir, MapNo);
            AutoSetValue();
            if (SelDType.ProType == zjgisProType.FTP ||
                        (ProductHelper.ProductConfig.ProType == zjgisProType.FTP && SelDType.ProType == zjgisProType.UnKnow))
            {
                ImpType = zjgisProType.FTP;
            }
            else if (SelDType.ProType == zjgisProType.Blob ||
                (ProductHelper.ProductConfig.ProType == zjgisProType.Blob && SelDType.ProType == zjgisProType.UnKnow))
            {
                ImpType = zjgisProType.Blob;
            }
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isLyr"></param>
        /// <param name="featureclass"></param>
        /// <param name="isOnlyMeta"></param>
        /// <returns></returns>
        public bool Import(bool isLyr,IFeatureClass featureclass,bool isOnlyMeta)
        {
            if (isLyr) return Import2Layer(featureclass, isOnlyMeta);
            else return Import2Table(featureclass, isOnlyMeta);
        }

        /// <summary>
        /// 导入一份产品数据到数据库中
        /// </summary>
        /// <param name="featureclass">空间数据图层</param>
        /// <param name="isOnlyMeta">是否只录元数据信息</param>
        /// <returns></returns>
        public bool Import2Layer(IFeatureClass featureclass,bool isOnlyMeta)
        {
            if (featureclass == null) return false;
            StringBuilder sb = new StringBuilder();
            sb.Append(F_MapNo + " = '{0}'");
            sb.Append(" and " + MetaHelper.SCRQ + " = to_date('{1}','yyyy-mm-dd')");
            sb.Append(" and " + MetaHelper.BLCFM + " = '{2}'");
            IQueryFilter flt = new QueryFilter();
            flt.SubFields = "ID";
            flt.WhereClause = string.Format(sb.ToString(), MapNo, SCDate.ToString("yyyy-MM-dd"), Scale);
            IFeatureCursor featcur = featureclass.Search(flt, false);
            IFeature feat = featcur.NextFeature();
            ID = Guid.NewGuid().ToString();
            if (feat != null)
            {
                if (!isOnlyMeta)
                {
                    LogHelper.WriteLog(new LogMessage() { Message = "元数据【" + MapNo + "】已存在，不能入库！", LogType = zjgisLogType.zjgisLogError });
                    return true;
                }
                ID = feat.get_Value(feat.Fields.FindField("ID")).ToString();
            }            
            bool suc = true;
            string wkt = Values[MetaHelper.GEOMETRY.ToUpper()];
            IGeometry geo = GetGeo(wkt);// GeometryHelper.ToGeometry(wkt);
            try
            {
                if (Files != null && Files.Count > 0 && !isOnlyMeta)
                {
                    if (ImpType == zjgisProType.FTP)
                    {
                        suc = UploadFile();
                    }
                    else if (ImpType == zjgisProType.Blob )
                    {
                        ProductHelper.DbConnect.Context.CreateTransaction();
                        XMIN = geo.Envelope.XMin;
                        YMIN = geo.Envelope.YMin;
                        XMAX = geo.Envelope.XMax;
                        YMAX = geo.Envelope.YMax;
                        suc = InsertFile();
                        if (suc) ProductHelper.DbConnect.Context.CommitTransaction();
                        else ProductHelper.DbConnect.Context.RollbackTransaction();
                    }
                }
                if (!suc) return suc;
                
                Dictionary<string, object> invalues = new Dictionary<string, object>();
                invalues.Add(MetaHelper.ID, ID);//
                foreach (KeyValuePair<string, string> kvp in Values)
                {
                    if (string.IsNullOrWhiteSpace(kvp.Value)) continue;
                    invalues.Add(kvp.Key, kvp.Value);
                }
                PlanningGIS.ArcGIS.FeatureClassHelper.InsertFeatures(featureclass, new List<IGeometry>()
                {
                    geo
                }, new List<Dictionary<string, object>>()
                {
                    invalues
                });
                return suc;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(new LogMessage() { Message = "产品[" + MapNo + "]入库失败:" + ex.Message, LogType = zjgisLogType.zjgisLogError });
                return false;
            }
        }

        /// <summary>
        /// 导入一份产品数据到数据库中
        /// </summary>
        /// <param name="featureclass">空间数据图层</param>
        /// <param name="isOnlyMeta">是否只录元数据信息</param>
        /// <returns></returns>
        public bool Import2Table(IFeatureClass featureclass, bool isOnlyMeta)
        {
            string sql = "";
            DataTable metatable = GetImpData(out sql);
            if (metatable == null) return false; //数据操作异常
            string metaid = Guid.NewGuid().ToString();
            if (metatable.Rows.Count > 0)
            {
                if (!isOnlyMeta)
                {
                    LogHelper.WriteLog(new LogMessage() { Message = "元数据【" + MapNo + "】已存在，不能入库！", LogType = zjgisLogType.zjgisLogError });
                    return true;
                }
                metaid = metatable.Rows[0][MetaHelper.ID].ToString();
            }            
            ProductHelper.DbConnect.Context.CreateTransaction();
            bool suc = true;
            try
            {
                if (Files != null && Files.Count > 0 && !isOnlyMeta)
                {
                    if (ImpType == zjgisProType.FTP)
                    {
                        suc = UploadFile();
                    }
                    else if (ImpType == zjgisProType.Blob )
                    {
                        suc = InsertFile();
                    }
                }
                if (!suc) return suc;
                if (featureclass != null && Values.ContainsKey(MetaHelper.GEOMETRY.ToUpper()) && !string.IsNullOrWhiteSpace(Values[MetaHelper.GEOMETRY.ToUpper()]))
                {
                    InsertOrUpdateGeometry(featureclass, Values[MetaHelper.GEOMETRY.ToUpper()], (metatable.Rows.Count > 0));
                }
                DataRow metaDataRow = null;
                if (metatable.Rows.Count > 0)
                    metaDataRow = metatable.Rows[0];
                else
                    metaDataRow = metatable.NewRow();
                foreach (KeyValuePair<string, string> kvp in Values)
                {
                    try
                    {
                        if (metatable.Columns[kvp.Key] != null && !string.IsNullOrEmpty(kvp.Value) && !string.IsNullOrWhiteSpace(kvp.Value))
                            metaDataRow[kvp.Key] = kvp.Value;
                    }
                    catch
                    {
                        LogHelper.WriteLog("字符" + kvp.Key + "赋值失败，值和字段类型不匹配");
                    }
                    
                }
                metaDataRow[MetaHelper.ID] = metaid;
                if (metatable.Rows.Count == 0)
                    metatable.Rows.Add(metaDataRow);
                ProductHelper.DbConnect.Context.UpdateDataTable(metatable, sql);
                metatable.AcceptChanges();
                metatable.Rows.Clear();
                return suc;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(new LogMessage() { Message = "产品[" + MapNo + "]入库失败:" + ex.Message, LogType = zjgisLogType.zjgisLogError });
                return false;
            }
            finally
            {
                if (suc)
                    ProductHelper.DbConnect.Context.CommitTransaction();
                else
                    ProductHelper.DbConnect.Context.RollbackTransaction();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="featureclass"></param>
        /// <param name="wkt"></param>
        /// <param name="update"></param>
        private void InsertOrUpdateGeometry(IFeatureClass featureclass, string wkt, bool update)
        {
            try
            {
                IGeometry geo = GetGeo(wkt);// GeometryHelper.ToGeometry(wkt);
                if (geo == null || geo.IsEmpty) return;
                if (update)
                {
                    IFeatureCursor cur = FeatureClassHelper.ExistFeatureCursor(featureclass, string.Format("{0}='{1}'", MetaHelper.METAID, ID), null);
                    if (cur != null)
                    {
                        IFeature f = cur.NextFeature();
                        f.Shape = geo;
                        f.Store();
                        return;
                    }
                }
                Dictionary<string, object> values = new Dictionary<string, object>();
                values.Add(MetaHelper.METAID, ID);
                values.Add(MetaHelper.METACODE, MapNo);
                FeatureClassHelper.InsertFeatures(featureclass, new List<IGeometry>() { geo }, new List<Dictionary<string, object>>() { values });
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(new LogMessage() { Message = "更新产品的空间位置失败:" + ex.Message, LogType = zjgisLogType.zjgisLogError });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="wkt"></param>
        /// <returns></returns>
        private IGeometry GetGeo(string wkt)
        {
            if (string.IsNullOrWhiteSpace(wkt)) return null;
            IXMLReader pReader = new XMLReader();
            IXMLStream pStream = new XMLStream();
            try
            {
                wkt = wkt.Replace("&lt;", "<");
                wkt = wkt.Replace("&gt;", ">");
                pStream.LoadFromString(wkt);
                pReader.ReadFrom(pStream as IStream);
                IXMLSerializer pSerializer = new XMLSerializer();
                IGeometry geo = pSerializer.ReadObject(pReader, null, null) as IGeometry;
                if (geo != null && !geo.IsEmpty)
                {
                    if (geo is IGeometryCollection)
                    {
                        List<IGeometry> geonews = new List<IGeometry>();
                        IGeometryCollection geos = geo as IGeometryCollection;
                        for (int i = 0; i < geos.GeometryCount; i++)
                        {
                            IGeometry g = geos.get_Geometry(i);
                            if (g != null && !g.IsEmpty)
                            {
                                g = Convertor.ConvertLow2High(g);
                                if (g != null && !g.IsEmpty) geonews.Add(g);
                            }
                        }
                        if (geonews != null && geonews.Count > 0) geo = GeometryHelper.Union(geonews);
                    }
                }
                return geo;
            }
            catch
            {
                LogHelper.WriteLog("空间图像构造失败，可能是节点数据太多。");
                return null;
            }            
        }

        /// <summary>
        /// 根据比例尺、生产日期、图号获取是否已存在产品数据
        /// </summary>
        /// <param name="sql">返回的查询语句</param>
        /// <returns></returns>
        private DataTable GetImpData(out string sql)
        {
            sql = "select * from " + SelDType.TableName + " where " + F_MapNo + " = :TH ";
            sql += " and " + MetaHelper.SCRQ + "= :SCRQ ";
            sql += " and " + MetaHelper.BLCFM + " = :BLCFM ";
            try
            {
                Dictionary<string, dynamic> wheres = new Dictionary<string, dynamic>();
                wheres.Add(":TH", MapNo);
                wheres.Add(":SCRQ", SCDate);
                wheres.Add(":BLCFM", Scale);
                return ProductHelper.DbConnect.Context.GetDataTable(sql, wheres);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(new LogMessage()
                {
                    Message = "执行【" + sql + "】失败:" + ex.Message,
                    LogType = zjgisLogType.zjgisLogError
                });
                return null;
            }
        }

        /// <summary>
        /// 上传文件到Ftp中
        /// </summary>
        /// <returns></returns>
        private bool UploadFile()
        {
            bool suc = true;
            try
            {
                foreach (string fi in Files)
                {
                    string fname = System.IO.Path.GetFileName(fi);
                    FileInfo finfo = new FileInfo(fi);
                    if (finfo.Directory.Name.Equals(MapNo)) fname = MapNo + "\\" + fname;
                    string p = ViewD4Helper.GetProductName(SelDType, SCDate, Scale.ToString(), fname);
                    
                    if (OSHelper.IsLocalIP(AppConfig.SystemConfig.FTP.Server))
                    {
                        p = ProductHelper.FtpPath + "\\" + p;
                        string dir = System.IO.Path.GetDirectoryName(p);
                        if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
                        File.Copy(fi, p, true);
                    }
                    else
                    {
                        suc = ProductHelper.Ftp.UploadFile(fi, "./" + p, false);
                    }
                    if (!suc)
                    {
                        LogHelper.WriteLog(new LogMessage() { Message = "文件上传失败：" + ProductHelper.Ftp.Message, LogType = zjgisLogType.zjgisLogError });
                        break;
                    }
                }
                return suc;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(new LogMessage() { Message = "文件上传失败：" + ex.Message, LogType = zjgisLogType.zjgisLogError });
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 将产品数据文件上传到数据库中
        /// </summary>
        /// <returns></returns>
        private bool InsertFile()
        {
            try
            {
                OracleCommand command = ProductHelper.DbConnect.Context.CreateCommand() as OracleCommand;
                command.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
                command.Parameters.Add(new OracleParameter("tempblob", OracleDbType.Blob)).Direction = ParameterDirection.Output;
                command.ExecuteNonQuery();
                OracleBlob blob = (OracleBlob)command.Parameters[0].Value;
                ViewD4Helper.Files2Oraclelob(Files.ToArray(), blob);
                StringBuilder sb = new StringBuilder();
                sb.Append("insert into MD_PRODUCT_SHAPE( ");
                sb.Append(PlanningGIS.MetaData.MetaHelper.ID).Append(", ");
                sb.Append(PlanningGIS.MetaData.MetaHelper.METAID).Append(", ");
                sb.Append(PlanningGIS.MetaData.MetaHelper.GEOMETRY).Append(", ");
                sb.Append(PlanningGIS.MetaData.MetaHelper.XMIN).Append(", ");
                sb.Append(PlanningGIS.MetaData.MetaHelper.YMIN).Append(", ");
                sb.Append(PlanningGIS.MetaData.MetaHelper.XMAX).Append(", ");
                sb.Append(PlanningGIS.MetaData.MetaHelper.YMAX);
                sb.Append(" )values( ");
                sb.Append(" :id,:metaid,:geometry,:xmin,:ymin,:xmax,:ymax ");
                sb.Append(" )");
                Dictionary<string, dynamic> wheres = new Dictionary<string, dynamic>();
                wheres.Add(":id", Guid.NewGuid().ToString());
                wheres.Add(":metaid", ID);
                wheres.Add(":geometry", blob);
                wheres.Add(":xmin", XMIN);
                wheres.Add(":ymin", YMIN);
                wheres.Add(":xmax", XMAX);
                wheres.Add(":ymax", YMAX);
                int count = ProductHelper.DbConnect.Context.ExecuteNonQuery(sb.ToString(), wheres);
                blob.Erase();
                blob.Close();
                return count > 0;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(new LogMessage()
                {
                    Message = "写入产品文件到数据库失败：" + ex.Message,
                    LogType = zjgisLogType.zjgisLogError
                });
                return false;
            }
        }


        /// <summary>
        /// 自动计算元数据中系统字段的值
        /// </summary>
        /// <param name="values">元数据信息</param>
        private void AutoSetValue()
        {
            //string th = Values[F_MapNo];
            //bool istf = SelDType.Rank == zjgisRankType.TF;
            //try
            //{
            //    List<Mapsheet> tfs = istf ? JHBHelper.GetMapsheets(th, Scale) : null;
            //    List<int> data = new List<int>();
            //    foreach (MetaField fd in currentRule.Fields)
            //    {
            //        if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueUnKnow) continue;
            //        if (istf)
            //        {
            //            if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueXEN)
            //                values[fd.Name] = Math.Round(tfs[4].Geometry.Envelope.XMax, 4).ToString();
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueYEN)
            //                values[fd.Name] = Math.Round(tfs[4].Geometry.Envelope.YMax, 4).ToString();
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueXWS)
            //                values[fd.Name] = Math.Round(tfs[4].Geometry.Envelope.XMin, 4).ToString();
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueYWS)
            //                values[fd.Name] = Math.Round(tfs[4].Geometry.Envelope.YMin, 4).ToString();
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueScale)
            //                values[fd.Name] = blc.ToString();
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTHWN)
            //                values[fd.Name] = tfs[0].Code;
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTHNorth)
            //                values[fd.Name] = tfs[1].Code;
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTHEN)
            //                values[fd.Name] = tfs[2].Code;
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTHWest)
            //                values[fd.Name] = tfs[3].Code;
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTH)
            //                values[fd.Name] = tfs[4].Code;
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTHEast)
            //                values[fd.Name] = tfs[5].Code;
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTHWS)
            //                values[fd.Name] = tfs[6].Code;
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTHSouth)
            //                values[fd.Name] = tfs[7].Code;
            //            else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTHES)
            //                values[fd.Name] = tfs[8].Code;
            //        }
            //        if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueNow)
            //            values[fd.Name] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //        else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueDataType)
            //            values[fd.Name] = ((MetaDataType)cmbDataType.SelectedItem).Rank.ToString();
            //        else if (fd.AutoValueType == zjgisAutoValueType.zjgisAutoValueTH)
            //            values[fd.Name] = th;
            //    };
            //}
            //catch (System.Exception ex)
            //{
            //    LogHelper.WriteErrorText("给【" + th + "】自动赋值失败：" + ex.Message);
            //}
        }

    }
}
